package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IMemberFinder;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.MemberModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.orhanobut.logger.Logger;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.net.URI;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/8/3.
 */
@EActivity(R.layout.activity_smrz_tongguo)
public class SMRZTGActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView tv_tongguo;
    @ViewById
    TextView txt_renzheng;
    @ViewById
    TextView tv_realname;
    @ViewById
    TextView tv_shenfenzheng;
    @ViewById
    SimpleDraweeView img_card1;
    @ViewById
    SimpleDraweeView img_card2;
    @ViewById
    SimpleDraweeView img_card3;
    @Bean
    GearsErrorHandler errorHandler;
    @RestService
    IMemberFinder memberFinder;
    MemberModel memberModel;
    MaterialDialogProgress progress;

    @AfterViews
    void created() {
        actionbar_title.setText("实名认证");
        initIconfont();
        StatusBarCompat.setStatusBarColor(this,ContextCompat.getColor(this, R.color.colorActionbar));

        memberModel = new MemberModel();
        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        getCurrentMember();
    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
        tv_tongguo.setTypeface(app.iconfont);
    }

    @Background
    void getCurrentMember() {
        ResultModel<MemberModel> result = memberFinder.getMember();
        showMemberResult(result);
    }

    @UiThread
    void showMemberResult(ResultModel<MemberModel> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    memberModel = res.getData();
                    if (memberModel.getPassengerauditstate().equals("2")) {
                        tv_tongguo.setTextColor(ContextCompat.getColor(this, R.color.colorFontOrange));
                        txt_renzheng.setText("实名认证正在审核");
                    } else {
                        tv_tongguo.setTextColor(ContextCompat.getColor(this, R.color.colorGreen));
                        txt_renzheng.setText("实名认证成功");
                    }
                    Uri uri1 = Uri.parse(memberModel.getIdphoto1() + RequestUrl.card);
                    Uri uri2 = Uri.parse(memberModel.getIdphoto2() + RequestUrl.card);
                    Uri uri3 = Uri.parse(memberModel.getHoldid() + RequestUrl.card);
                    img_card1.setImageURI(uri1);
                    img_card2.setImageURI(uri2);
                    img_card3.setImageURI(uri3);
                    String realName = memberModel.getFname();
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < realName.length() - 1; i++) {
                        sb.append("*");
                    }
                    tv_realname.setText(realName.substring(0, 1) + sb.toString());
                    String driverid = memberModel.getDriverid();
                    if (driverid != null && !driverid.equals("")) {
                        String license = driverid.substring(0, 3) + "************" + driverid.substring(driverid.length() - 3, driverid.length());
                        tv_shenfenzheng.setText(license);
                    } else {
                        tv_shenfenzheng.setText("等待审核");
                    }
                }
            }
        }
        progress.dismiss();
    }


    @Click
    void back_icon() {
        finish();
    }
}
